<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button>点击</button>
    <script>
        const button = document.querySelector('button')
        function Subject() {
            let listener = null
            let to = null
            let canrun = true
            return {
                pushlish(val) {

                    if (listener) {

                        clearTimeout(to)
                        to = setTimeout(() => {
                            listener(val)
                        }, 1000)

                    }
                },
                //节流
                throttlepublish(val){
                    
                   
                        if (canrun) {
                            canrun = false
                        setTimeout(() => {
                            listener(val)
                        }, 1000)
                        setTimeout(() => {
                            canrun = true
                        }, 5000)
                        }
                      
                       
                }
                ,
                subscribe(cb) {
                    listener = cb
                }
            }
        }
        const subject = Subject()
        subject.subscribe((msg)=>{
            console.log(msg);
        })

        button.onclick = ()=>{
            subject.throttlepublish('点击')
        }
    </script>
</body>

</html>